/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CrearNuevoGrupoDialog.java
 *
 * Created on Jun 18, 2013, 6:04:39 PM
 */
package pe.edu.unmsm.cinfo.siga.ui.ventanas;

import java.awt.Color;
import java.awt.Component;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import org.jdesktop.application.Action;
import org.jdesktop.observablecollections.ObservableCollections;
import pe.edu.unmsm.cinfo.siga.datos.Aula;
import pe.edu.unmsm.cinfo.siga.datos.Dia;
import pe.edu.unmsm.cinfo.siga.datos.Horario;
import pe.edu.unmsm.cinfo.siga.datos.HorarioPK;
import pe.edu.unmsm.cinfo.siga.datos.ModuloGrupo;
import pe.edu.unmsm.cinfo.siga.ui.utils.DateToHoraConverter;
import pe.edu.unmsm.cinfo.siga.ui.utils.Hora;
import pe.edu.unmsm.cinfo.siga.ui.utils.ValidadorGrupo;

/**
 *
 * @author Carito
 */
public class CrearNuevoGrupoDialog extends javax.swing.JDialog {

    public class DateToHoraRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (value instanceof Date) {
                Date d = (Date) value;
                setText(DateToHoraConverter.get().convertForward(d).toString());
            }
            return this;
        }
    }

    public class DateToHoraEditor extends DefaultCellEditor {

        private JTextField textField;

        public DateToHoraEditor() {
            this(new JTextField());
        }

        private DateToHoraEditor(JTextField textField) {
            super(textField);
            this.textField = textField;
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            if (value instanceof Date) {
                super.getTableCellEditorComponent(table, value, isSelected, row, column);
                textField.setText(DateToHoraConverter.get().convertForward((Date) value).toString());
                return textField;
            } else {
                return super.getTableCellEditorComponent(table, value, isSelected, row, column);
            }
        }

        @Override
        public Object getCellEditorValue() {
            return DateToHoraConverter.get().convertReverse(new Hora(textField.getText()));
        }

        @Override
        public boolean stopCellEditing() {
            try {
                getCellEditorValue();
                return super.stopCellEditing();
            } catch (IllegalArgumentException iae) {
                marcarEditor();
                return false;
            }
        }

        private void marcarEditor() {
            textField.setBorder(new LineBorder(Color.RED, 1));
        }
    }
    
    public class AulaEditor extends DefaultCellEditor {
        public AulaEditor(){
            super(aulasComboBox);
        }

        /*@Override
        public Object getCellEditorValue() {
            if(super.getCellEditorValue() instanceof Aula)
                return ((Aula)(super.getCellEditorValue())).getAulCod();
            else
                return super.getCellEditorValue();
        }*/
        
        
    }
    
    private boolean entityConfirmed = false;
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

    /** Creates new form CrearNuevoGrupoDialog */
    public CrearNuevoGrupoDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        establecerOyentes();
    }

    private void establecerOyentes() {
        diasList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                changeSupport.firePropertyChange("diaDisponibleSeleccionado",
                        !isDiaDisponibleSeleccionado(),
                        isDiaDisponibleSeleccionado());
            }
        });
        tablaHorario.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                changeSupport.firePropertyChange("diaAsignadoSeleccionado",
                        !isDiaAsignadoSeleccionado(),
                        isDiaAsignadoSeleccionado());
            }
        });
    }

    public boolean isDiaDisponibleSeleccionado() {
        return diasList.getSelectedIndex() != -1;
    }

    public boolean isDiaAsignadoSeleccionado() {
        return tablaHorario.getSelectedRow() != -1;
    }

    public boolean isEntityConfirmed() {
        return entityConfirmed;
    }

    public void setEntityConfirmed(boolean entityConfirmed) {
        this.entityConfirmed = entityConfirmed;
    }

    public ModuloGrupo getGrupoEditado() {
        return grupoEditado;
    }

    public void setGrupoEditado(ModuloGrupo mgrupo) {
        assert mgrupo != null;
        if (mgrupo.getHorarioList() == null) {
            mgrupo.setHorarioList(new LinkedList<Horario>());
        }
        ModuloGrupo old = this.grupoEditado;
        this.grupoEditado = mgrupo;
        changeSupport.firePropertyChange("grupoEditado", old, mgrupo);
        setHorarioGrupoEditado(mgrupo.getHorarioList());
        filtrarDiasDisponibles();
    }

    public List<Horario> getHorarioGrupoEditado() {
        return horarioGrupoEditado;
    }

    private void setHorarioGrupoEditado(List<Horario> horarioGrupoEditado) {
        List<Horario> old = getHorarioGrupoEditado();
        this.horarioGrupoEditado = ObservableCollections.observableList(horarioGrupoEditado);
        changeSupport.firePropertyChange("horarioGrupoEditado", old, getHorarioGrupoEditado());
    }

    @Override
    public void addPropertyChangeListener(PropertyChangeListener l) {
        changeSupport.addPropertyChangeListener(l);
    }

    @Override
    public void removePropertyChangeListener(PropertyChangeListener l) {
        changeSupport.removePropertyChangeListener(l);
    }

    private void filtrarDiasDisponibles() {
        for (Horario horario : getHorarioGrupoEditado()) {
            listaDias.remove(horario.getDia());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        grupoEditado = null;
        horarioGrupoEditado = null;
        aulasComboBox = new javax.swing.JComboBox();
        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("AplicacionSIGAPU").createEntityManager();
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pe.edu.unmsm.cinfo.siga.ui.Main.class).getContext().getResourceMap(CrearNuevoGrupoDialog.class);
        consultaDias = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("consultaDias.query")); // NOI18N
        listaDias = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(consultaDias.getResultList());
        consultaAulas = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("consultaAulas.query")); // NOI18N
        listaAulas = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : consultaAulas.getResultList();
        jLabel1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        diasList = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        botonQuitar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablaHorario = new javax.swing.JTable();
        botonAgregar = new javax.swing.JButton();
        txtNombreModulo = new javax.swing.JTextField();
        txtHoras = new javax.swing.JTextField();
        inicioDateChooser = new com.toedter.calendar.JDateChooser();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        aulasComboBox.setName("aulasComboBox"); // NOI18N

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listaAulas, aulasComboBox);
        bindingGroup.addBinding(jComboBoxBinding);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText("Codigo:"); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel5.setText("Horario:"); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel4.setText("horas"); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel6.setText("Seleccione días para horario:"); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        txtCodigo.setName("txtCodigo"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${grupoEditado.modgCod}"), txtCodigo, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        diasList.setName("diasList"); // NOI18N

        org.jdesktop.swingbinding.JListBinding jListBinding = org.jdesktop.swingbinding.SwingBindings.createJListBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listaDias, diasList);
        jListBinding.setDetailBinding(org.jdesktop.beansbinding.ELProperty.create("${diaNom}"));
        bindingGroup.addBinding(jListBinding);

        jScrollPane1.setViewportView(diasList);

        jLabel2.setText("Nombre:"); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Duracion:"); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(pe.edu.unmsm.cinfo.siga.ui.Main.class).getContext().getActionMap(CrearNuevoGrupoDialog.class, this);
        botonQuitar.setAction(actionMap.get("desasignarAction")); // NOI18N
        botonQuitar.setIcon(resourceMap.getIcon("botonQuitar.icon")); // NOI18N
        botonQuitar.setName("botonQuitar"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tablaHorario.setName("tablaHorario"); // NOI18N
        tablaHorario.getTableHeader().setReorderingAllowed(false);

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${horarioGrupoEditado}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, tablaHorario);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${dia.diaNom}"));
        columnBinding.setColumnName("Dia.dia Nom");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${horIni}"));
        columnBinding.setColumnName("Hor Ini");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${horFin}"));
        columnBinding.setColumnName("Hor Fin");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${aulCod}"));
        columnBinding.setColumnName("Aul Cod");
        columnBinding.setColumnClass(pe.edu.unmsm.cinfo.siga.datos.Aula.class);
        jTableBinding.setSourceNullValue(Collections.EMPTY_LIST);
        jTableBinding.setSourceUnreadableValue(Collections.EMPTY_LIST);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane2.setViewportView(tablaHorario);
        tablaHorario.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tablaHorario.columnModel.title0")); // NOI18N
        tablaHorario.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tablaHorario.columnModel.title1")); // NOI18N
        tablaHorario.getColumnModel().getColumn(1).setCellEditor(new DateToHoraEditor());
        tablaHorario.getColumnModel().getColumn(1).setCellRenderer(new DateToHoraRenderer());
        tablaHorario.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tablaHorario.columnModel.title2")); // NOI18N
        tablaHorario.getColumnModel().getColumn(2).setCellEditor(new DateToHoraEditor());
        tablaHorario.getColumnModel().getColumn(2).setCellRenderer(new DateToHoraRenderer());
        tablaHorario.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("tablaHorario.columnModel.title3")); // NOI18N
        tablaHorario.getColumnModel().getColumn(3).setCellEditor(new AulaEditor());

        botonAgregar.setAction(actionMap.get("asignarAction")); // NOI18N
        botonAgregar.setIcon(resourceMap.getIcon("botonAgregar.icon")); // NOI18N
        botonAgregar.setName("botonAgregar"); // NOI18N

        txtNombreModulo.setEditable(false);
        txtNombreModulo.setName("txtNombreModulo"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, this, org.jdesktop.beansbinding.ELProperty.create("${grupoEditado.modCod.modDesc}"), txtNombreModulo, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceNullValue("--");
        binding.setSourceUnreadableValue("--");
        bindingGroup.addBinding(binding);

        txtHoras.setEditable(false);
        txtHoras.setName("txtHoras"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${grupoEditado.modCod.modDura}"), txtHoras, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        inicioDateChooser.setName("inicioDateChooser"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${grupoEditado.modgFini}"), inicioDateChooser, org.jdesktop.beansbinding.BeanProperty.create("date"));
        bindingGroup.addBinding(binding);

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jTextField1.setEditable(false);
        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jButton1.setAction(actionMap.get("cancelarAction")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("grabarAction")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 223, Short.MAX_VALUE)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtHoras, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4))
                            .addComponent(txtNombreModulo, javax.swing.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(inicioDateChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                                .addGap(92, 92, 92)
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(133, 133, 133))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(11, 11, 11)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(botonAgregar)
                                    .addComponent(botonQuitar)))
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE))
                    .addComponent(jLabel5)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(txtHoras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNombreModulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(inicioDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(21, 21, 21)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(11, 11, 11)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addComponent(botonAgregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonQuitar))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action(enabledProperty = "diaDisponibleSeleccionado")
    public void asignarAction() {
        agregarHorarioAGrupo(obtenerDiaSeleccionado());
    }

    private Dia obtenerDiaSeleccionado() {
        int indice = diasList.getSelectedIndex();
        return listaDias.get(indice);
    }

    private void agregarHorarioAGrupo(Dia dia) {
        getHorarioGrupoEditado().add(nuevoHorario(dia));
        listaDias.remove(dia);
    }

    private Horario nuevoHorario(Dia dia) {
        Horario horario = new Horario();
        horario.setHorarioPK(new HorarioPK());
        horario.getHorarioPK().setDiaCod(dia.getDiaCod());
        horario.setAulCod(null);
        horario.setDia(dia);
        horario.setHorIni(new GregorianCalendar(1970, 0, 1, 8, 0, 0).getTime());
        horario.setHorFin(new GregorianCalendar(1970, 0, 1, 9, 0, 0).getTime());
        return horario;
    }

    @Action(enabledProperty = "diaAsignadoSeleccionado")
    public void desasignarAction() {
        quitarHorarioAGrupo(obtenerHorarioSeleccionado());
    }

    private Horario obtenerHorarioSeleccionado() {
        int indice = tablaHorario.convertRowIndexToModel(tablaHorario.getSelectedRow());
        return getHorarioGrupoEditado().get(indice);
    }

    private void quitarHorarioAGrupo(Horario horario) {
        getHorarioGrupoEditado().remove(horario);
        listaDias.add(horario.getDia());
    }

    @Action
    public void grabarAction() {
        ValidadorGrupo validador = new ValidadorGrupo(getGrupoEditado());
        if(validador.esValido()){
            setEntityConfirmed(true);
            setVisible(false);
        }else{
            JOptionPane.showMessageDialog(this, validador.getMensaje(), "SIGA-CINFO", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    @Action
    public void cancelarAction() {
        setEntityConfirmed(false);
        setVisible(false);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox aulasComboBox;
    private javax.swing.JButton botonAgregar;
    private javax.swing.JButton botonQuitar;
    private javax.persistence.Query consultaAulas;
    private javax.persistence.Query consultaDias;
    private javax.swing.JList diasList;
    private javax.persistence.EntityManager entityManager;
    private pe.edu.unmsm.cinfo.siga.datos.ModuloGrupo grupoEditado;
    private java.util.List<Horario> horarioGrupoEditado;
    private com.toedter.calendar.JDateChooser inicioDateChooser;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private java.util.List<Aula> listaAulas;
    private java.util.List<Dia> listaDias;
    private javax.swing.JTable tablaHorario;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtHoras;
    private javax.swing.JTextField txtNombreModulo;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
